package com.app.fenak;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainOptionsAdapter extends BaseAdapter {

	// number of options on main options view
	private final int optionsCount; 
	private Context context;
	private ViewHolder[] holders;
	
	public MainOptionsAdapter(Context context) {
		super();
		this.context = context;
		
		String[] optionsTitles = context.getResources().getStringArray(R.array.options_titles);
		TypedArray optionsIcons  = context.getResources().obtainTypedArray(R.array.options_drawables_ids);
		
		optionsCount = optionsTitles.length;
		holders = new ViewHolder[optionsCount];
		for (int i = 0; i < optionsCount; i++) {
			holders[i] = new ViewHolder();
			holders[i].title = optionsTitles[i];
			int temp = optionsIcons.getResourceId(i, -1);
			if(temp != -1)
				holders[i].icon = temp;
			else
				holders[i].icon = R.drawable.globe;
		}
		
	}

	@Override
	public int getCount() {
		return optionsCount;
	}

	@Override
	public Object getItem(int pos) {
		return holders[pos];
	}

	@Override
	public long getItemId(int pos) {
		return R.layout.option_row_list;
	}

	@Override
	public View getView(int pos, View view, ViewGroup arg2) {
		
		view = (LinearLayout) view;
		
		if(view == null){
			 LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			 view = (LinearLayout) vi.inflate(R.layout.option_row_list, null, true);
		} 
		
		((ImageView) view.findViewById(R.id.option_icon)).setImageDrawable(context.getResources().getDrawable(holders[pos].icon));
		((TextView) view.findViewById(R.id.option_title)).setText(holders[pos].title);
		
		return view;
	}

	class ViewHolder {
		int icon;
		String  title;
	}
}
